package com.banciyuan.bcywebview.biz.main.mineinfo.mark;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.activity.BaseActivity;
import com.banciyuan.bcywebview.base.helper.ActionbarHelper;
import com.banciyuan.bcywebview.base.helper.data.UserDataHelper;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;
import com.banciyuan.bcywebview.utils.string.StringUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by squall on 2016/1/20.
 */
public class MarkCircleActivity extends BaseActivity {

    public static final int TYPE_WORK = 0;
    public static final int TYPE_TAG = 1;

    private List<Fragment> flist = new ArrayList<>();
    private FragmentManager fragmentManager;
    private ViewPager mPager;
    private MyAdapter mAdapter;
    private TabLayout mTabLayout;
    private ActionbarHelper mActionbarHelper;
    private View mActionBarView;
    private int tabItem;
    private String[] tabs;

    private String uid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.marklist_detail_viewpager_layout);
        initArgs();
        getFragment();
        initActionbar();
        initUi();
        initAction();
    }

    @Override
    protected void initArgs() {
        tabs = new String[]{getString(R.string.acg_works), getString(R.string.detail_tag)};
        fragmentManager = getSupportFragmentManager();
        tabItem = getIntent().getIntExtra(gotoUtil.INTENT_VALUE_ONE, TYPE_WORK);
        uid = getIntent().getStringExtra(gotoUtil.INTENT_VALUE_TWO);
    }

    private void getFragment() {

        Bundle bundle = new Bundle();
        bundle.putString("uid", uid);

        Fragment mineAcgFragment = getAcgFragment(bundle);
        flist.add(mineAcgFragment);

        MyTagFragment myTagFragment = new MyTagFragment();
        myTagFragment.setArguments(bundle);
        flist.add(myTagFragment);

    }

    private OtherAcgFragment getAcgFragment(Bundle bundle) {
        if (StringUtil.notNullEqual(uid, UserDataHelper.getInstance(this).getUid())) {
            MyAcgFragment myAcgFragment = new MyAcgFragment();
            myAcgFragment.setArguments(bundle);
            return myAcgFragment;
        } else {
            OtherAcgFragment otherAcgFragment = new OtherAcgFragment();
            otherAcgFragment.setArguments(bundle);
            return otherAcgFragment;
        }
    }

    @Override
    protected void initActionbar() {
        mActionBarView = findViewById(R.id.base_action_bar);
        mActionbarHelper = new ActionbarHelper(this, mActionBarView);
        if (StringUtil.notNullEqual(uid, UserDataHelper.getInstance(this).getUid())) {
            mActionbarHelper.setTitle(getString(R.string.my_focus_circle));
        } else {
            mActionbarHelper.setTitle(getString(R.string.it_focus_circle));
        }
    }

    @Override
    protected void initUi() {
        mAdapter = new MyAdapter(fragmentManager);
        mTabLayout = (TabLayout) findViewById(R.id.tablayout);
        mPager = (ViewPager) findViewById(R.id.pager);
        mPager.setOffscreenPageLimit(2);
        mPager.setAdapter(mAdapter);
        mTabLayout.setupWithViewPager(mPager);
        if (tabItem == TYPE_TAG)
            mPager.setCurrentItem(1);
    }

    class MyAdapter extends FragmentPagerAdapter {

        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return flist.get(position);
        }

        @Override
        public int getCount() {
            return flist.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return tabs[position];
        }
    }


    @Override
    protected void onSaveInstanceState(Bundle outState) {

    }
}
